﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ZX.WebApp.WorkFlow
{
    public partial class WorkFlowAccredStatistics : System.Web.UI.Page
    {
        public string UserId = "";
        //调用流程服务接口实例
        public CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = null;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                UserId = this.Page.User.Identity.Name;
                //实例化流程服务接口
                workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
                bind();
            }
        }
        private void bind()
        {
            DataTable source = workFlowServiceClient.GetOwnAccreditUser(UserId); //当前操作人代理状况
            gvAccredPersonal.DataSource = source;
            gvAccredPersonal.DataBind();

            DataTable table = workFlowServiceClient.GetOtherAccreditUser(UserId);//其它操作人代理状况
            gvAccredOther.DataSource = table;
            gvAccredOther.DataBind();

        }
        /// <summary>
        /// 新增流程代理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WorkFlow/WorkFlowAccredAdd.aspx?Status=10");
        }
        /// <summary>
        /// 是否收回流转中流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hlkTakeback_Click(object sender, EventArgs e)
        {
            workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
            GridViewRow row = (sender as LinkButton).Parent.Parent as GridViewRow;
            string accreditToUserId = ((Label)row.FindControl("lblAccreditToUserId")).Text;
            string accreditFromUserId = ((Label)row.FindControl("txtAccreditFromUserId")).Text;
            bool reust = false;
            if (((CheckBox)row.FindControl("ckAccredit")).Checked == true)
            {
               reust = workFlowServiceClient.RemoveAccredit(accreditFromUserId, accreditToUserId, true,DateTime.Now);
            }
            else
            {
               reust = workFlowServiceClient.RemoveAccredit(accreditFromUserId, accreditToUserId, false, DateTime.Now);
            }
            if (reust)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('收回代理成功')</script>");
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('收回代理失败')</script>");
            }
            bind();
        }
    }
}